package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.SkillDao;
import com.jbp.model.Skill;
import com.jbp.model.SkillCate;
import com.jbp.model.SkillUser;
import com.jbp.service.SkillCateService;
import com.jbp.service.SkillService;
import com.jbp.service.SkillUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.List;
import java.util.Map;

/**
 * 技能(Skill)表服务实现类
 * @author cc
 * @since 2020-11-19 17:06:55
 */
@Service
public class SkillServiceImpl extends ServiceImpl<SkillDao, Skill> implements SkillService {
    @Autowired
    private SkillCateService skillCateService;
    @Autowired
    private SkillUserService skillUserService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Skill> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<Skill> page = this.page(
                new Query<Skill>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**所有技能*/
    @Override
    public List<SkillCate> all() {
        List<SkillCate> skillCates = skillCateService.list(new QueryWrapper<SkillCate>()
                .eq("status",1)
                .orderByAsc("sort")
        );
        for (SkillCate skillCate : skillCates) {
            if (skillCate.getId()==1) {
                //热门
                skillCate.setSkills(hot());
            } else {
                skillCate.setSkills(list(new QueryWrapper<Skill>()
                        .eq("status", 1)
                        .eq("cate_id", skillCate.getId())
                        .orderByAsc("sort")));
            }
        }
        return skillCates;
    }

    @Override
    public List<Skill> hot() {
        return list(new QueryWrapper<Skill>()
                .eq("status", 1)
                .ne("hot", 0)
                .orderByDesc("hot")
        );
    }

    @Override
    public List<Skill> search(String name) {
        QueryWrapper<Skill> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank("name")) {
            queryWrapper.like("name", name);
        }
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        return list(queryWrapper);
    }

    @Override
    public List<SkillCate> applyAll(Integer userId) {
        List<SkillCate> all = all();
        for (SkillCate skillCate : all) {
            for (Skill skill : skillCate.getSkills()) {
                SkillUser one = skillUserService.getOne(new QueryWrapper<SkillUser>()
                        .eq("skill_id", skill.getId())
                        .eq("user_id", userId)
                );
                if (one!=null) {
                    skill.setStatus(one.getStatus());
                }
            }
        }
        return all;
    }


}